package mybatis.parse;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义sql解析器
 */
public class MyParser {
    /**
     * 参数位置Map
     */
    private static Map<Integer, String> parameterPositionMap;

    /**
     * sql解析
     */
    public static String parse(String sql) {
        parameterPositionMap = new HashMap<>();

        StringBuilder stringBuilder = new StringBuilder();

        int i = 1;

        String[] sqls = sql.split("#\\{");
        for (String str : sqls) {
            if (str.contains("}")) {
                String[] strs = str.split("}");
                stringBuilder.append("?");
                if (strs.length > 1) {
                    stringBuilder.append(strs[1]);
                }

                parameterPositionMap.put(i, strs[0]);
                i++;
            } else {
                stringBuilder.append(str);
            }
        }

        return stringBuilder.toString();
    }

    public static Map<Integer, String> getParameterPositionMap() {
        return parameterPositionMap;
    }
}